//
//  LoginViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/15.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LoginViewController.h"


@interface LoginViewController ()

@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *passwordTF;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) UIImageView *phoneIcon;
@property (nonatomic, strong) UIImageView *lockIcon;
@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UIView *line3;
@property (nonatomic, strong) UIButton *secretButton;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    
//    self.navigationController -> rootvc
//    self.navigationController = @[registervc,firstvc]

    
//    LoginApi
//    （login） -> yes
//    register - >uid {@(NO)}
//    
}

- (void)viewDidAppear:(BOOL)animated {
    if ([DTCEXRACT_OBJECT(REGISTER) isEqualToString:@"YES"]) {
        RegisterViewController *vc = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)refreshBaseControlValue
{

    self.navigationBar.hidden = YES;

    [self layoutSubviews];
//    [self.view addSubview:self.iconImageView];
}


- (void)layoutSubviews {
    
    CGFloat edge = px1080Width(100);
//    CGFloat high = px1920Height(270);
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(px1920Height(300));
        make.centerX.equalTo(self.view);
//        make.left.equalTo(self.view).with.offset(px1080Width(366));
//        make.right.equalTo(self.view).with.offset(-px1080Width(366));
        make.height.mas_equalTo(px1920Height(300));
        make.width.mas_equalTo(px1080Width(300));
    }];
    

    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).with.offset(px1920Height(210));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    

    [self.phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(edge);
        make.bottom.mas_equalTo(self.line1).with.offset(-px1920Height(16));
        make.width.mas_equalTo(px1080Width(60));
        make.height.mas_equalTo(px1920Height(60));
    }];
    
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line1).with.offset(-px1920Height(16));
        make.height.mas_equalTo(px1920Height(60));
        make.left.equalTo(self.phoneIcon.mas_right).with.offset(px1080Width(36));
        make.right.equalTo(self.view).with.offset(-edge);
    }];
    
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line1.mas_bottom).with.offset(px1920Height(157));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    
    
    [self.lockIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(edge);
        make.bottom.mas_equalTo(self.line2).with.offset(-px1920Height(16));
        make.width.mas_equalTo(px1080Width(60));
        make.height.mas_equalTo(px1920Height(60));
    }];
    
    [self.secretButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(px1920Height(60));
        make.width.mas_equalTo(px1080Width(60));
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.height.mas_equalTo(px1920Height(60));
        make.left.equalTo(self.lockIcon.mas_right).with.offset(px1080Width(36));
        make.right.equalTo(self.secretButton.mas_left).with.offset(-edge);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2.mas_bottom).with.offset(px1920Height(81));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).with.offset(px1920Height(66));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_bottom).with.offset(-px1920Height(170));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(px1920Height(2));
    }];
    
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.line3);
        make.width.mas_equalTo(px1080Width(300));
        make.height.mas_equalTo(px1920Height(42));
    }];
}


#pragma mark -- 懒加载
- (UIImageView *)logoImageView
{

    if(!_logoImageView)
    {
        _logoImageView = [[UIImageView alloc]init];
        _logoImageView.image = [UIImage imageNamed:@"logo"];
        [self.view addSubview:_logoImageView];

    }
    return _logoImageView;
}


- (UITextField *)phoneTF
{
    if(!_phoneTF)
    {
        _phoneTF = [[UITextField alloc]init];
        _phoneTF.placeholder = @"请输入手机号码";
        _phoneTF.keyboardType = UIKeyboardTypePhonePad;
        [self.view addSubview:_phoneTF];
    }
    return _phoneTF;
}


- (UITextField *)passwordTF
{
    if(!_passwordTF)
    {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.secureTextEntry = YES;
        [self.view addSubview:_passwordTF];
    }
    return _passwordTF;
}

- (UIImageView *)phoneIcon
{
    if(!_phoneIcon)
    {
        _phoneIcon = [[UIImageView alloc]init];
        _phoneIcon.image = [UIImage imageNamed:@"icon_phone"];
        _phoneIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_phoneIcon];
    }
    return _phoneIcon;
}
- (UIImageView *)lockIcon
{
    if(!_lockIcon)
    {
        _lockIcon = [[UIImageView alloc]init];
        _lockIcon.image = [UIImage imageNamed:@"icon_icon_password"];
        _lockIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_lockIcon];
    }
    return _lockIcon;
}

- (UIView *)line1
{
    if(!_line1)
    {
        _line1 = [[UIView alloc]init];
        _line1.backgroundColor = [UIColor colorWithHexString:@"#CACACA"];
        [self.view addSubview:_line1];
    }
    return _line1;
}
- (UIView *)line2
{
    if(!_line2)
    {
        _line2 = [[UIView alloc]init];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#CACACA"];
        [self.view addSubview:_line2];

    }
    return _line2;
}

- (UIView *)line3
{
    if(!_line3)
    {
        _line3 = [[UIView alloc]init];
        _line3.backgroundColor = [UIColor colorWithHexString:@"#7D8FC1"];
        [self.view addSubview:_line3];
        
    }
    return _line3;
}

- (UIButton *)secretButton
{
    if(!_secretButton)
    {
        _secretButton = [[UIButton alloc]init];
        [_secretButton setImage:[UIImage imageNamed:@"icon_invisible_nor"] forState:UIControlStateNormal];
        [_secretButton setImage:[UIImage imageNamed:@"icon_visible_nor"] forState:UIControlStateSelected];
        [_secretButton addTarget:self action:@selector(didClickSecretButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_secretButton];
    }
    return _secretButton;
}

- (UIButton *)loginButton
{
    if(!_loginButton)
    {
        _loginButton = [[UIButton alloc]init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
//        [_loginButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"button_login_nor"] forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"button_login_sel"] forState:UIControlStateHighlighted];
        [_loginButton addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}
- (UIButton *)registerButton
{
    if(!_registerButton)
    {
        _registerButton = [[UIButton alloc]init];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor colorWithHexString:@"#3E76F6"] forState:UIControlStateNormal];
//        [_registerButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_registerButton setBackgroundImage:[UIImage imageNamed:@"button_next_nor"] forState:UIControlStateNormal];
        [_registerButton setBackgroundImage:[UIImage imageNamed:@"button_next_sel"] forState:UIControlStateHighlighted];
        [_registerButton addTarget:self action:@selector(didClickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_registerButton];
    }
    return _registerButton;
}


- (UIButton *)forgetButton
{
    if(!_forgetButton)
    {
        _forgetButton = [[UIButton alloc]init];
        [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [_forgetButton setTitleColor:[UIColor colorWithHexString:@"#7D8FC1"] forState:UIControlStateNormal];
        [_forgetButton setBackgroundColor:[UIColor whiteColor]];
        [_forgetButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_forgetButton addTarget:self action:@selector(didClickForgetPasswordButton) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_forgetButton];
    }
    return _forgetButton;
}



#pragma mark -- 事件
- (void)didClickSecretButton {
    
    self.secretButton.selected = !self.secretButton.selected;
    self.passwordTF.secureTextEntry = !self.secretButton.selected;
}

- (void)didClickForgetPasswordButton {
    ForgetPasswordViewController *vc = [[ForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didClickLoginButton {
    
    if (![_phoneTF.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![_passwordTF.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-20位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showInfoWithStatus:@"正在登录..."];
    });
    
    [self loginActionStart];
}

- (void)loginActionStart {
    
    LoginApi *api = [[LoginApi alloc]initWithDic:@{@"username":self.phoneTF.text,@"password":self.passwordTF.text}];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [request responseJSONObject];
        if ([dict[@"errcode"] intValue] == 0) {
            
            [SVProgressHUD showSuccessWithStatus:@"您已安全登录！"];
            DTCSAVE_OBJECT(self.phoneTF.text , USERNAME);
            DTCSAVE_OBJECT(self.passwordTF.text, PASSWORD);
            DTCSAVE_OBJECT(dict[@"data"][@"uid"], UID);
            DTCSAVE_OBJECT(dict[@"data"][@"token"], TOKEN);
            DTCSAVE_OBJECT(dict[@"data"][@"image"], IMAGE);
            DTCSAVE_OBJECT(@"YES", @"Login");   // 登录状态保存来设置根控制器
            DTCSAVE_OBJECT(@"YES", LOGIN);
            
            [self GetUserInfo];
        } else {
            [SVProgressHUD showInfoWithStatus:dict[@"errmsg"]];
        }
    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showInfoWithStatus:@"网络有误"];
    }];
}

- (void)GetUserInfo {
    
    GetUserInfoApi * userInfoApi = [[GetUserInfoApi alloc] init];
    [userInfoApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [userInfoApi analyseResponseObject];
        DTCSAVE_OBJECT(@"YES", LOGIN);
        [[AppDelegate defaultService]setRootViewController];
    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showErrorWithStatus:@"获取用户信息失败！"];
    }];
}


- (void)didClickRegisterButton {
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
